package com.weigee.ktv.v.adapter.rv;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.l.d.app.library.adapter.RvListAdapter;
import com.l.d.app.library.util.CalendarUtil;
import com.l.d.app.library.util.ParseUtil;
import com.l.d.app.library.view.stiky_header.StickyRecyclerHeadersAdapter;
import com.weigee.ktv.R;
import com.weigee.ktv.m.entity.TransactionHistoryEntity;
import com.weigee.ktv.m.entity.WrapHolder;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import skin.support.widget.SkinCompatTextView;

/**
 * Created by linde on 2017/04/26.
 * <p/>
 */
public class TransactionHistoryAdapter extends RvListAdapter<TransactionHistoryEntity, TransactionHistoryAdapter.Holder> implements StickyRecyclerHeadersAdapter<TransactionHistoryAdapter.StickyHolder> {
    @NonNull
    @Override
    public List<TransactionHistoryEntity> onCreateNewEmptyList() {
        return new ArrayList<>();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        final Context context = parent.getContext();
        final View itemView = LayoutInflater.from(context).inflate(R.layout.transactionhistory_frag___rv_item, parent, false);
        return new Holder(itemView);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final TransactionHistoryEntity entity = getItem(position);
        if (entity == null) {
            return;
        }
        holder.tvMsg.setText(entity.getMsg());
        holder.tvDate.setText(CalendarUtil.stringFormat(entity.getTranTime(), "MM-dd HH:mm", Locale.getDefault()));

        final int money = entity.getTranMoney();
        holder.tvDetail.setText(String.valueOf((money > 0 ? "+" : "") + ParseUtil.numPoint(money, 2)));
    }

    @Override
    public long getHeaderId(int position) {
        final TransactionHistoryEntity entity = getItem(position);
        if (entity == null) {
            return position;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(entity.getTranTime());
        return calendar.get(Calendar.YEAR) * 100 + calendar.get(Calendar.MONTH);
    }

    @Override
    public StickyHolder onCreateHeaderViewHolder(ViewGroup parent) {
        final Context context = parent.getContext();
        final View itemView = LayoutInflater.from(context).inflate(R.layout.transactionhistory_frag___sticky_item, parent, false);
        return new StickyHolder(itemView);
    }

    @Override
    public void onBindHeaderViewHolder(StickyHolder holder, int position) {
        final TransactionHistoryEntity entity = getItem(position);
        if (entity == null) {
            return;
        }
        holder.tvDate.setText(CalendarUtil.stringFormat(entity.getTranTime(), "yyyy年MM月", Locale.getDefault()));
    }

    static class Holder extends WrapHolder {
        @BindView(R.id.tv_charge_msg)
        SkinCompatTextView tvMsg;
        @BindView(R.id.tv_charge_date)
        SkinCompatTextView tvDate;
        @BindView(R.id.tv_charge_detail)
        SkinCompatTextView tvDetail;

        private Holder(View itemView) {
            super(itemView);
        }
    }

    static class StickyHolder extends WrapHolder {
        @BindView(R.id.tv_date)
        SkinCompatTextView tvDate;

        private StickyHolder(View itemView) {
            super(itemView);
        }
    }
}
